<?php
namespace app\api\model;

/*客服模型*/
use app\api\model\CommonModel;

class KfReply extends CommonModel
{
    private $table_name = 'kf_reply';
    public function __construct()
    {
       parent::__construct();
    }
    /**初始化参数
     * @param $args [client_id,token,enc_string,nickname]
     * @return array
     */
    private function default_param($args)
    {
        $default = [
            "title"     => "",
            "content" => "",
            "sort"      => "",
        ];
        return array_merge($default,$args);
    }


    //获取所有自动回复
    public function total($args = [])
    {
        /**
         * $args参数说明
         * args.id 根据回复id
         * args.keyword 根据title,content模糊查询
         * pageLimit 每页显示多少条数据 默认15条
         */

        $pageLimit = isset($args['pageLimit']) && intval($args['pageLimit']) > 0 ? $args['pageLimit'] : 15;
        $where = [];
        if(isset($args['id']) && intval($args['id']) > 0)
        {
            $where['id'] = intval($args['id']);
        }

        if(isset($args['keyword']) && $args['keyword'])
        {
            $where['title|content'] = ['like','%'.$args['keyword'].'%'];
        }
        //获取数据
        $data = null;
        if(!empty($where))
        {
            $data = db($this->table_name)->field('id,title')->where($where)->order('sort DESC, id ASC')->paginate($pageLimit);
        }
        else
        {
            $data = db($this->table_name)->field('id,title')->order('sort DESC, id ASC')->paginate($pageLimit);
        }

        return $data;
    }

    /**获取一个自动回复记录
     * @param array $args key:[id,title,keywords], 中括号表示任选一个可以查询
     * @return bool
     */
    public function info($args)
    {

        if(empty($args)) return false;
        //参数合并
        $args = $this->default_param($args);
        //如果没有查询信息，返回空
        if((!isset($args['id']) || empty($args['id'] ))
            && empty($args['title'] )
            && empty($args['keywords'])
        ) return false;

        //根据id,title,content其中一个查询
        if(!empty($args['id']))
        {
            $filter = ['id'=>$args['id']];
        }
        elseif(!empty($args['title']))
        {
            $filter = ['title'=>['like','%'.$args['title'].'%']];
        }
        elseif(!empty($args['keywords']))
        {
            $filter = [
                'keywords'=>['like','%'.$args['keywords'].'%'],
            ];
        }

        if(empty($filter)) return false;

        if(empty($args['keywords']))
        {
            //返回查询信息
           return  db($this->table_name)->where($filter)->order('id DESC')->find();
        }
        else
        {
            $data = db($this->table_name)->where(['title'=>$args['keywords']])->whereOr('keywords','like','%'.$args['keywords'].'%')->order('id DESC')->select();
        }

            foreach($data as $k=>$v)
            {
                //分割问题对应的关键词
                $keywordArr = explode(",",$v['keywords']);
                if(!empty($keywordArr))
                {
                    //查找到关键字
                    if(in_array($args['keywords'],$keywordArr))
                    {
                        //直接返回这条回复
                        return $data[$k];
                    }

                }
            }
        return null;
    }


}
